package com.sunlg.smarthome.ui.curtain;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.data.entity.Curtain;

import java.util.List;

/**
 * Created by sunlg on 2017/5/3.
 */

public class CurtainAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<Curtain> mCurtainList;

    public CurtainAdapter(Context context, List<Curtain> curtainList) {
        this.mContext = context;
        this.mCurtainList = curtainList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_curtain, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
        final Curtain curtain = mCurtainList.get(position);

        itemViewHolder.tvDesc.setText(curtain.getDescription());
        itemViewHolder.tvStatus.setText(curtain.getStatus()+"%");

        itemViewHolder.rlCurtainItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("curtain", curtain);
                bundle.putInt("pos", position);
                ((BaseActivity)mContext).overlay(CurtainActivity.class, bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mCurtainList.size();
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder{

        TextView tvDesc;
        TextView tvStatus;
        RelativeLayout rlCurtainItem;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvDesc = (TextView)itemView.findViewById(R.id.tv_desc);
            tvStatus = (TextView)itemView.findViewById(R.id.tv_status);
            rlCurtainItem = (RelativeLayout)itemView.findViewById(R.id.rl_curtain_item);
        }
    }
}
